package com.learn.project2.service.wx.home;

import com.learn.project2.bean.*;
import com.learn.project2.mapper.*;
import com.learn.project2.model.vo.wx.home.HomeIndexVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/8
 * \* Time: 21:06
 * \* Description:
 */
@Service
public class HomeServiceImpl2 implements HomeService2 {

    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketAdMapper adMapper;
    @Autowired
    MarketGrouponMapper grouponMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketTopicMapper topicMapper;

    
    /** 
     * @description: 首页-商品列表
     * @param: [] 
     * @return: com.learn.project2.model.vo.wx.home.HomeIndexVO2 
     * @author 王力
     * @date: 2022-01-09 14:44:27
     */ 
    @Override
    public HomeIndexVO2 homeIndex() {
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria()
                .andDeletedEqualTo(false).andIsNewEqualTo(true);
        goodsExample.setOrderByClause("id ASC limit 10");
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(goodsExample);
        HomeIndexVO2 homeIndexVO2 = new HomeIndexVO2();
        homeIndexVO2.setNewGoodsList(marketGoods);
        goodsExample.clear();

        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.setOrderByClause("id ASC limit 3");
        couponExample.createCriteria().andDeletedEqualTo(false);
        List<MarketCoupon> coupons = couponMapper.selectByExample(couponExample);
        homeIndexVO2.setCouponList(coupons);

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.setOrderByClause("id ASC limit 10");
        categoryExample.createCriteria().andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MarketCategory> categories = categoryMapper.selectByExample(categoryExample);
        homeIndexVO2.setChannel(categories);
        categoryExample.clear();

        MarketAdExample adExample = new MarketAdExample();
        adExample.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true);
        adExample.setOrderByClause("id ASC limit 10");
        List<MarketAd> adList = adMapper.selectByExample(adExample);
        homeIndexVO2.setBanner(adList);
        adExample.clear();

        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        brandExample.setOrderByClause("id ASC limit 10");
        List<MarketBrand> brandList = brandMapper.selectByExample(brandExample);
        homeIndexVO2.setBrandList(brandList);
        brandExample.clear();

        MarketGrouponExample grouponExample = new MarketGrouponExample();
        grouponExample.createCriteria().andDeletedEqualTo(false);
        grouponExample.setOrderByClause("id ASC limit 10");
        List<MarketGroupon> groupons = grouponMapper.selectByExample(grouponExample);
        homeIndexVO2.setGrouponList(groupons);
        grouponExample.clear();

        MarketGoodsExample goodsExample2 = new MarketGoodsExample();
        goodsExample2.createCriteria().andIsHotEqualTo(true)
                .andDeletedEqualTo(false).andIsNewEqualTo(true);
        goodsExample2.setOrderByClause("id ASC limit 10");
        List<MarketGoods> marketGoods2 = goodsMapper.selectByExample(goodsExample2);
        homeIndexVO2.setHotGoodsList(marketGoods2);
        goodsExample2.clear();

        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        topicExample.setOrderByClause("id ASC limit 10");
        List<MarketTopic> topics = topicMapper.selectByExample(topicExample);
        homeIndexVO2.setTopicList(topics);

        List<HomeIndexVO2.GoodsHome> goodsHome = new ArrayList<>();

        MarketCategoryExample categoryExample1 = new MarketCategoryExample();
        categoryExample1.createCriteria();
        categoryExample1.setOrderByClause("id ASC limit 10");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(categoryExample1);
        for (MarketCategory category : marketCategories) {
            HomeIndexVO2.GoodsHome home = new HomeIndexVO2.GoodsHome();
            home.setId(category.getId());
            home.setName(category.getName());
            MarketGoodsExample goodsExample1 = new MarketGoodsExample();
            goodsExample1.createCriteria().andCategoryIdEqualTo(category.getId());
            goodsExample1.setOrderByClause("id ASC limit 4");
            List<MarketGoods> goods = goodsMapper.selectByExample(goodsExample1);
            home.setGoodsList(goods);
            goodsHome.add(home);
        }
        homeIndexVO2.setFloorGoodsList(goodsHome);

        return homeIndexVO2;
    }
}
